package com.guodu.dao.impl;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.PurchaseProductionPlanDao;
import com.guodu.entity.PurchaseProductionPlan;
import com.guodu.entity.PurchaseproductionStatus;
import com.guodu.entity.Units;
import com.guodu.vo.PurchaseProductionPlanQueryVo;
import com.guodu.vo.PurchaseProductionPlanVo;


@Repository("purchaseProductionPlanDao")
public class PurchaseProductionPlanDaoImpl extends BaseDao implements PurchaseProductionPlanDao {



	@Override
	public void save(PurchaseProductionPlan purchaseProductionPlan) {
		// TODO Auto-generated method stub
		super.save(purchaseProductionPlan);
	}


	@Override
	public long findSizeByVo(PurchaseProductionPlanQueryVo purchaseProductionPlanQueryVo,Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select count(*) from PurchaseProductionPlan m where 1=1");
		if(purchaseProductionPlanQueryVo!=null){
			if (purchaseProductionPlanQueryVo.getFunitsId()!=null) {
				hql.append("    and exists (select 1 from Units u where u.unitsId = m.unitsId and u.funitsId  ="+purchaseProductionPlanQueryVo.getFunitsId()+" )");
			}
			if (purchaseProductionPlanQueryVo.getUnitsId()!=null && !"".equals(purchaseProductionPlanQueryVo.getUnitsId())) {
				hql.append(" and exists(select 1 from UserInfo u where u.userId=m.createPeople and u.unitsId="+purchaseProductionPlanQueryVo.getUnitsId()+")");
			}
			if (purchaseProductionPlanQueryVo.getTypes()!=null && !"".equals(purchaseProductionPlanQueryVo.getTypes())) {
				hql.append("    and types like '%"+purchaseProductionPlanQueryVo.getTypes()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getMaterialCode()!=null && !"".equals(purchaseProductionPlanQueryVo.getMaterialCode())) {
				hql.append("    and materialCode like '%"+purchaseProductionPlanQueryVo.getMaterialCode()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getMaterialName()!=null && !"".equals(purchaseProductionPlanQueryVo.getMaterialName())) {
				hql.append("    and materialName like '%"+purchaseProductionPlanQueryVo.getMaterialName()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getTexture()!=null && !"".equals(purchaseProductionPlanQueryVo.getTexture())) {
				hql.append("    and texture like '%"+purchaseProductionPlanQueryVo.getTexture()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getDrawingnum()!=null && !"".equals(purchaseProductionPlanQueryVo.getDrawingnum())) {
				hql.append("    and drawingnum like '%"+purchaseProductionPlanQueryVo.getDrawingnum()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getProcurementMethod()!=null && !"".equals(purchaseProductionPlanQueryVo.getProcurementMethod())) {
				hql.append("    and procurementMethod like '%"+purchaseProductionPlanQueryVo.getProcurementMethod()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getEmergency()!=null && !"".equals(purchaseProductionPlanQueryVo.getEmergency())) {
				hql.append("    and emergency like '%"+purchaseProductionPlanQueryVo.getEmergency().trim()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getBeginDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getBeginDate())) {
				hql.append("    and arrivalTime >= TO_DATE('"+purchaseProductionPlanQueryVo.getBeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (purchaseProductionPlanQueryVo.getEndDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getEndDate())) {
				hql.append("    and arrivalTime <= TO_DATE('"+purchaseProductionPlanQueryVo.getEndDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (purchaseProductionPlanQueryVo.getTbbeginDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getTbbeginDate())) {
				hql.append("    and createTime >= TO_DATE('"+purchaseProductionPlanQueryVo.getTbbeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (purchaseProductionPlanQueryVo.getTbendDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getTbendDate())) {
				hql.append("    and createTime <= TO_DATE('"+purchaseProductionPlanQueryVo.getTbendDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(purchaseProductionPlanQueryVo.getStatus()!=null&&purchaseProductionPlanQueryVo.getStatus().trim().length()>0&&!purchaseProductionPlanQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+purchaseProductionPlanQueryVo.getStatus());
			}
			if(purchaseProductionPlanQueryVo.getMonths()!=null&&purchaseProductionPlanQueryVo.getMonths().trim().length()>0){
				hql.append(" and TO_DATE(months,'yyyy-mm') >= TO_DATE('"+purchaseProductionPlanQueryVo.getMonths()+"','yyyy-mm')");
			}
			if(purchaseProductionPlanQueryVo.getEmonths()!=null&&purchaseProductionPlanQueryVo.getEmonths().trim().length()>0){
				hql.append(" and TO_DATE(months,'yyyy-mm') <= TO_DATE('"+purchaseProductionPlanQueryVo.getEmonths()+"','yyyy-mm')");
			}
		}
		if(units==null){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==1){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==0){
			hql.append("and (status=0 or status=1 or status=2 or status=3 or status=4)");
		}
		return super.findInt(hql.toString());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PurchaseProductionPlan> findByVo(
			PurchaseProductionPlanQueryVo purchaseProductionPlanQueryVo,Units units, int curPage, int pageSize) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from PurchaseProductionPlan m where 1=1");
		if(purchaseProductionPlanQueryVo!=null){
			if (purchaseProductionPlanQueryVo.getFunitsId()!=null) {
				hql.append("    and exists (select 1 from Units u where u.unitsId = m.unitsId and u.funitsId  ="+purchaseProductionPlanQueryVo.getFunitsId()+" )");
			}
			if (purchaseProductionPlanQueryVo.getUnitsId()!=null && !"".equals(purchaseProductionPlanQueryVo.getUnitsId())) {
				hql.append(" and exists(select 1 from UserInfo u where u.userId=m.createPeople and u.unitsId="+purchaseProductionPlanQueryVo.getUnitsId()+")");
			}
			if (purchaseProductionPlanQueryVo.getIndustry()!=null && !"".equals(purchaseProductionPlanQueryVo.getIndustry())) {
				hql.append("    and exits(select 1 from Units u where u.unitsId = m.unitsId and u.industry like '%"+purchaseProductionPlanQueryVo.getIndustry()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getTypes()!=null && !"".equals(purchaseProductionPlanQueryVo.getTypes())) {
				hql.append("    and types like '%"+purchaseProductionPlanQueryVo.getTypes()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getMaterialCode()!=null && !"".equals(purchaseProductionPlanQueryVo.getMaterialCode())) {
				hql.append("    and materialCode like '%"+purchaseProductionPlanQueryVo.getMaterialCode()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getMaterialName()!=null && !"".equals(purchaseProductionPlanQueryVo.getMaterialName())) {
				hql.append("    and materialName like '%"+purchaseProductionPlanQueryVo.getMaterialName()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getTexture()!=null && !"".equals(purchaseProductionPlanQueryVo.getTexture())) {
				hql.append("    and texture like '%"+purchaseProductionPlanQueryVo.getTexture()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getDrawingnum()!=null && !"".equals(purchaseProductionPlanQueryVo.getDrawingnum())) {
				hql.append("    and drawingnum like '%"+purchaseProductionPlanQueryVo.getDrawingnum()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getProcurementMethod()!=null && !"".equals(purchaseProductionPlanQueryVo.getProcurementMethod())) {
				hql.append("    and procurementMethod like '%"+purchaseProductionPlanQueryVo.getProcurementMethod()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getEmergency()!=null && !"".equals(purchaseProductionPlanQueryVo.getEmergency())) {
				hql.append("    and emergency like '%"+purchaseProductionPlanQueryVo.getEmergency().trim()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getBeginDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getBeginDate())) {
				hql.append("    and arrivalTime >= TO_DATE('"+purchaseProductionPlanQueryVo.getBeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (purchaseProductionPlanQueryVo.getEndDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getEndDate())) {
				hql.append("    and arrivalTime <= TO_DATE('"+purchaseProductionPlanQueryVo.getEndDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (purchaseProductionPlanQueryVo.getTbbeginDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getTbbeginDate())) {
				hql.append("    and createTime >= TO_DATE('"+purchaseProductionPlanQueryVo.getTbbeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (purchaseProductionPlanQueryVo.getTbendDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getTbendDate())) {
				hql.append("    and createTime <= TO_DATE('"+purchaseProductionPlanQueryVo.getTbendDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(purchaseProductionPlanQueryVo.getStatus()!=null&&purchaseProductionPlanQueryVo.getStatus().trim().length()>0&&!purchaseProductionPlanQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+purchaseProductionPlanQueryVo.getStatus());
			}
			if(purchaseProductionPlanQueryVo.getMonths()!=null&&purchaseProductionPlanQueryVo.getMonths().trim().length()>0){
				hql.append(" and TO_DATE(months,'yyyy-mm') >= TO_DATE('"+purchaseProductionPlanQueryVo.getMonths()+"','yyyy-mm')");
			}
			if(purchaseProductionPlanQueryVo.getEmonths()!=null&&purchaseProductionPlanQueryVo.getEmonths().trim().length()>0){
				hql.append(" and TO_DATE(months,'yyyy-mm') <= TO_DATE('"+purchaseProductionPlanQueryVo.getEmonths()+"','yyyy-mm')");
			}
		}
		if(units==null){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==1){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==0){
			hql.append("and (status=0 or status=1 or status=2 or status=3 or status=4)");
		}
		hql.append(" order by purchaseId desc");
		return super.findPages(hql.toString(), curPage, pageSize);
	}


	@Override
	public PurchaseProductionPlan findById(String purchaseId) {
		// TODO Auto-generated method stub
		return (PurchaseProductionPlan) super.findById(PurchaseProductionPlan.class, purchaseId);
	}


	@Override
	public void update(PurchaseProductionPlan purchaseProductionPlan) {
		// TODO Auto-generated method stub
		super.update(purchaseProductionPlan);
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<PurchaseproductionStatus> findStatus(String purchaseId) {
		// TODO Auto-generated method stub
		return super.findByProperty(PurchaseproductionStatus.class, "purchaseId", purchaseId);
	}


	@Override
	public void changeStatus(List<PurchaseproductionStatus> entities) {
		// TODO Auto-generated method stub
		super.saveAll(entities);
	}


	@Override
	public void examine(String ids, Long status) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update PurchaseProductionPlan set status =");
		hql.append(status);
		hql.append(" where purchaseId in (");
		hql.append(ids);
		hql.append(")");
		super.executeUpdate(hql.toString());
	}


	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		super.deleteAllById(PurchaseProductionPlan.class, delIds);
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<PurchaseProductionPlan> findAllByVo(
			PurchaseProductionPlanQueryVo purchaseProductionPlanQueryVo, Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from PurchaseProductionPlan m where 1=1");
		if(purchaseProductionPlanQueryVo!=null){
			if (purchaseProductionPlanQueryVo.getFunitsId()!=null) {
				hql.append("    and exists (select 1 from Units u where u.unitsId = m.unitsId and u.funitsId  ="+purchaseProductionPlanQueryVo.getFunitsId()+" )");
			}
			if (purchaseProductionPlanQueryVo.getUnitsId()!=null && !"".equals(purchaseProductionPlanQueryVo.getUnitsId())) {
				hql.append(" and exists(select 1 from UserInfo u where u.userId=m.createPeople and u.unitsId="+purchaseProductionPlanQueryVo.getUnitsId()+")");
			}
			if (purchaseProductionPlanQueryVo.getIndustry()!=null && !"".equals(purchaseProductionPlanQueryVo.getIndustry())) {
				hql.append("    and exits(select 1 from Units u where u.unitsId = m.unitsId and u.industry like '%"+purchaseProductionPlanQueryVo.getIndustry()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getTypes()!=null && !"".equals(purchaseProductionPlanQueryVo.getTypes())) {
				hql.append("    and types like '%"+purchaseProductionPlanQueryVo.getTypes()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getMaterialCode()!=null && !"".equals(purchaseProductionPlanQueryVo.getMaterialCode())) {
				hql.append("    and materialCode like '%"+purchaseProductionPlanQueryVo.getMaterialCode()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getMaterialName()!=null && !"".equals(purchaseProductionPlanQueryVo.getMaterialName())) {
				hql.append("    and materialName like '%"+purchaseProductionPlanQueryVo.getMaterialName()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getTexture()!=null && !"".equals(purchaseProductionPlanQueryVo.getTexture())) {
				hql.append("    and texture like '%"+purchaseProductionPlanQueryVo.getTexture()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getDrawingnum()!=null && !"".equals(purchaseProductionPlanQueryVo.getDrawingnum())) {
				hql.append("    and drawingnum like '%"+purchaseProductionPlanQueryVo.getDrawingnum()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getProcurementMethod()!=null && !"".equals(purchaseProductionPlanQueryVo.getProcurementMethod())) {
				hql.append("    and procurementMethod like '%"+purchaseProductionPlanQueryVo.getProcurementMethod()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getEmergency()!=null && !"".equals(purchaseProductionPlanQueryVo.getEmergency())) {
				hql.append("    and emergency like '%"+purchaseProductionPlanQueryVo.getEmergency().trim()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getBeginDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getBeginDate())) {
				hql.append("    and arrivalTime >= TO_DATE('"+purchaseProductionPlanQueryVo.getBeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (purchaseProductionPlanQueryVo.getEndDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getEndDate())) {
				hql.append("    and arrivalTime <= TO_DATE('"+purchaseProductionPlanQueryVo.getEndDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (purchaseProductionPlanQueryVo.getTbbeginDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getTbbeginDate())) {
				hql.append("    and createTime >= TO_DATE('"+purchaseProductionPlanQueryVo.getTbbeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (purchaseProductionPlanQueryVo.getTbendDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getTbendDate())) {
				hql.append("    and createTime <= TO_DATE('"+purchaseProductionPlanQueryVo.getTbendDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(purchaseProductionPlanQueryVo.getStatus()!=null&&purchaseProductionPlanQueryVo.getStatus().trim().length()>0&&!purchaseProductionPlanQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+purchaseProductionPlanQueryVo.getStatus());
			}
			if(purchaseProductionPlanQueryVo.getMonths()!=null&&purchaseProductionPlanQueryVo.getMonths().trim().length()>0){
				hql.append(" and TO_DATE(months,'yyyy-mm') >= TO_DATE('"+purchaseProductionPlanQueryVo.getMonths()+"','yyyy-mm')");
			}
			if(purchaseProductionPlanQueryVo.getEmonths()!=null&&purchaseProductionPlanQueryVo.getEmonths().trim().length()>0){
				hql.append(" and TO_DATE(months,'yyyy-mm') <= TO_DATE('"+purchaseProductionPlanQueryVo.getEmonths()+"','yyyy-mm')");
			}
		}
		if(units==null){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==1){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==0){
			hql.append("and (status=0 or status=1 or status=2 or status=3 or status=4)");
		}
		hql.append("order by createTime desc");
		return super.find(hql.toString());
	}


	@Override
	public PurchaseProductionPlanVo findAllMoney(PurchaseProductionPlanQueryVo purchaseProductionPlanQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select sum(estimatedAmount),sum(1) from  PurchaseProductionPlan m where 1=1");
		if(purchaseProductionPlanQueryVo!=null){
			if (purchaseProductionPlanQueryVo.getFunitsId()!=null) {
				hql.append("    and exists (select 1 from Units u where u.unitsId = m.unitsId and u.funitsId  ="+purchaseProductionPlanQueryVo.getFunitsId()+" )");
			}
			if (purchaseProductionPlanQueryVo.getUnitsId()!=null && !"".equals(purchaseProductionPlanQueryVo.getUnitsId())) {
				hql.append(" and exists(select 1 from UserInfo u where u.userId=m.createPeople and u.unitsId="+purchaseProductionPlanQueryVo.getUnitsId()+")");
			}
			if (purchaseProductionPlanQueryVo.getIndustry()!=null && !"".equals(purchaseProductionPlanQueryVo.getIndustry())) {
				hql.append("    and exits(select 1 from Units u where u.unitsId = m.unitsId and u.industry like '%"+purchaseProductionPlanQueryVo.getIndustry()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getTypes()!=null && !"".equals(purchaseProductionPlanQueryVo.getTypes())) {
				hql.append("    and types like '%"+purchaseProductionPlanQueryVo.getTypes()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getMaterialCode()!=null && !"".equals(purchaseProductionPlanQueryVo.getMaterialCode())) {
				hql.append("    and materialCode like '%"+purchaseProductionPlanQueryVo.getMaterialCode()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getMaterialName()!=null && !"".equals(purchaseProductionPlanQueryVo.getMaterialName())) {
				hql.append("    and materialName like '%"+purchaseProductionPlanQueryVo.getMaterialName()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getTexture()!=null && !"".equals(purchaseProductionPlanQueryVo.getTexture())) {
				hql.append("    and texture like '%"+purchaseProductionPlanQueryVo.getTexture()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getDrawingnum()!=null && !"".equals(purchaseProductionPlanQueryVo.getDrawingnum())) {
				hql.append("    and drawingnum like '%"+purchaseProductionPlanQueryVo.getDrawingnum()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getProcurementMethod()!=null && !"".equals(purchaseProductionPlanQueryVo.getProcurementMethod())) {
				hql.append("    and procurementMethod like '%"+purchaseProductionPlanQueryVo.getProcurementMethod()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getEmergency()!=null && !"".equals(purchaseProductionPlanQueryVo.getEmergency())) {
				hql.append("    and emergency like '%"+purchaseProductionPlanQueryVo.getEmergency().trim()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getBeginDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getBeginDate())) {
				hql.append("    and arrivalTime >= TO_DATE('"+purchaseProductionPlanQueryVo.getBeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (purchaseProductionPlanQueryVo.getEndDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getEndDate())) {
				hql.append("    and arrivalTime <= TO_DATE('"+purchaseProductionPlanQueryVo.getEndDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (purchaseProductionPlanQueryVo.getTbbeginDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getTbbeginDate())) {
				hql.append("    and createTime >= TO_DATE('"+purchaseProductionPlanQueryVo.getTbbeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (purchaseProductionPlanQueryVo.getTbendDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getTbendDate())) {
				hql.append("    and createTime <= TO_DATE('"+purchaseProductionPlanQueryVo.getTbendDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(purchaseProductionPlanQueryVo.getStatus()!=null&&purchaseProductionPlanQueryVo.getStatus().trim().length()>0&&!purchaseProductionPlanQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+purchaseProductionPlanQueryVo.getStatus());
			}
			if(purchaseProductionPlanQueryVo.getMonths()!=null&&purchaseProductionPlanQueryVo.getMonths().trim().length()>0){
				hql.append(" and TO_DATE(months,'yyyy-mm') >= TO_DATE('"+purchaseProductionPlanQueryVo.getMonths()+"','yyyy-mm')");
			}
			if(purchaseProductionPlanQueryVo.getEmonths()!=null&&purchaseProductionPlanQueryVo.getEmonths().trim().length()>0){
				hql.append(" and TO_DATE(months,'yyyy-mm') <= TO_DATE('"+purchaseProductionPlanQueryVo.getEmonths()+"','yyyy-mm')");
			}
		}
		if(units==null){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==1){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==0){
			hql.append("and (status=0 or status=1 or status=2 or status=3 or status=4)");
		}
		PurchaseProductionPlanVo vo  = new PurchaseProductionPlanVo();
		List<Object[]> objs = super.find(hql.toString());
		if(objs.size()>0){
			DecimalFormat df = new DecimalFormat("##,###.##");
			try {
				if(objs.get(0)[0]!=null){
				vo.setAllMoney1(df.format(df.parse(objs.get(0)[0].toString())));
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}
		return vo;
	}


	@Override
	public void examin(
			PurchaseProductionPlanQueryVo purchaseProductionPlanQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update PurchaseProductionPlan m set status=1 where 1=1");
		if(purchaseProductionPlanQueryVo!=null){
			if (purchaseProductionPlanQueryVo.getFunitsId()!=null) {
				hql.append("    and exists (select 1 from Units u where u.unitsId = m.unitsId and u.funitsId  ="+purchaseProductionPlanQueryVo.getFunitsId()+" )");
			}
			if (purchaseProductionPlanQueryVo.getUnitsId()!=null && !"".equals(purchaseProductionPlanQueryVo.getUnitsId())) {
				hql.append(" and exists(select 1 from UserInfo u where u.userId=m.createPeople and u.unitsId="+purchaseProductionPlanQueryVo.getUnitsId()+")");
			}
			if (purchaseProductionPlanQueryVo.getIndustry()!=null && !"".equals(purchaseProductionPlanQueryVo.getIndustry())) {
				hql.append("    and exits(select 1 from Units u where u.unitsId = m.unitsId and u.industry like '%"+purchaseProductionPlanQueryVo.getIndustry()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getTypes()!=null && !"".equals(purchaseProductionPlanQueryVo.getTypes())) {
				hql.append("    and types like '%"+purchaseProductionPlanQueryVo.getTypes()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getMaterialCode()!=null && !"".equals(purchaseProductionPlanQueryVo.getMaterialCode())) {
				hql.append("    and materialCode like '%"+purchaseProductionPlanQueryVo.getMaterialCode()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getMaterialName()!=null && !"".equals(purchaseProductionPlanQueryVo.getMaterialName())) {
				hql.append("    and materialName like '%"+purchaseProductionPlanQueryVo.getMaterialName()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getTexture()!=null && !"".equals(purchaseProductionPlanQueryVo.getTexture())) {
				hql.append("    and texture like '%"+purchaseProductionPlanQueryVo.getTexture()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getDrawingnum()!=null && !"".equals(purchaseProductionPlanQueryVo.getDrawingnum())) {
				hql.append("    and drawingnum like '%"+purchaseProductionPlanQueryVo.getDrawingnum()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getProcurementMethod()!=null && !"".equals(purchaseProductionPlanQueryVo.getProcurementMethod())) {
				hql.append("    and procurementMethod like '%"+purchaseProductionPlanQueryVo.getProcurementMethod()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getEmergency()!=null && !"".equals(purchaseProductionPlanQueryVo.getEmergency())) {
				hql.append("    and emergency like '%"+purchaseProductionPlanQueryVo.getEmergency().trim()+"%'");
			}
			if (purchaseProductionPlanQueryVo.getBeginDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getBeginDate())) {
				hql.append("    and arrivalTime >= TO_DATE('"+purchaseProductionPlanQueryVo.getBeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (purchaseProductionPlanQueryVo.getEndDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getEndDate())) {
				hql.append("    and arrivalTime <= TO_DATE('"+purchaseProductionPlanQueryVo.getEndDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (purchaseProductionPlanQueryVo.getTbbeginDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getTbbeginDate())) {
				hql.append("    and createTime >= TO_DATE('"+purchaseProductionPlanQueryVo.getTbbeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (purchaseProductionPlanQueryVo.getTbendDate()!=null && !"".equals(purchaseProductionPlanQueryVo.getTbendDate())) {
				hql.append("    and createTime <= TO_DATE('"+purchaseProductionPlanQueryVo.getTbendDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(purchaseProductionPlanQueryVo.getStatus()!=null&&purchaseProductionPlanQueryVo.getStatus().trim().length()>0&&!purchaseProductionPlanQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+purchaseProductionPlanQueryVo.getStatus());
			}
			if(purchaseProductionPlanQueryVo.getMonths()!=null&&purchaseProductionPlanQueryVo.getMonths().trim().length()>0){
				hql.append(" and TO_DATE(months,'yyyy-mm') >= TO_DATE('"+purchaseProductionPlanQueryVo.getMonths()+"','yyyy-mm')");
			}
			if(purchaseProductionPlanQueryVo.getEmonths()!=null&&purchaseProductionPlanQueryVo.getEmonths().trim().length()>0){
				hql.append(" and TO_DATE(months,'yyyy-mm') <= TO_DATE('"+purchaseProductionPlanQueryVo.getEmonths()+"','yyyy-mm')");
			}
		}
		if(units==null){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==1){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==0){
			hql.append("and (status=0 or status=1 or status=2 or status=3 or status=4)");
		}
		hql.append("and status=0");
		super.executeUpdate(hql.toString());
	}

}
